<?php

	function getLineGraph($x_axis,$y_axis,$id,$width="1100",$height="250",$sort=TRUE){
	
		if(count($x_axis)!=count($y_axis) || count($x_axis)<3) return;
		
		foreach($x_axis as $k=>$val){
			$x_axis[$k]=(float)formatNumber($x_axis[$k]);
			//$x_axis[$k]=formatNumber($x_axis[$k]);
		}
		
		foreach($y_axis as $k=> $val){
			$y_axis[$k]=(float)formatNumber($val);
			//$y_axis[$k]=formatNumber($y_axis[$k]);
		}
		
		if($sort==TRUE){
			array_multisort($x_axis, $y_axis);
		}
	 	$xaxis=json_encode($x_axis);
		$yaxis=json_encode($y_axis);
		$html=<<<EOT
			<script>
				$(document).ready(function(){
					var lineChartData = {
						labels: jQuery.parseJSON('{$xaxis}'),
						datasets: [
							{
								fillColor: "rgba(220,220,220,0.5)",
								strokeColor: "rgba(220,220,220,1)",
								pointColor: "rgba(220,220,220,1)",
								pointStrokeColor: "#fff",
								data: jQuery.parseJSON('{$yaxis}')
							}
						]

					}

					var myLine = new Chart(document.getElementById("{$id}").getContext("2d")).Line(lineChartData);
				});
			</script>
			<canvas id="$id" class="chart-holder" width="{$width}" height="{$height}"></canvas>
EOT;
		return $html;
	}
	
	function getLineGraph_flot($dataSet, $id, $onClickArgument="", $height='300px', $width='800px', $timeLine=false ){
	/*
		OPTTIONS:
		If $timeLine is passed as true, it will plot time in the x-axis, by default it is false
		
		DATA FORMAT:
		$data=[
				"lines"=>[
							[[0, 3], [4, 8], [8, 5], [9, 13]], //[[x1,y1],[x2,y2]....]
							[[2, 8], [6, 9], [3, 6], [1, 89]],
							[[12, 23], [85, 50], [60, 75], [25, 30]]
							
						],
				"bars"=>[
							[[0, 3], [4, 8], [8, 5], [9, 13]],
							[[2, 8], [6, 9], [3, 6], [1, 89]],
							[[12, 23], [85, 50], [60, 75], [25, 30]]
						],
			];
		
		Interactivity:
		$onClickArgument is the "" by default and there will be no interactivity, if some other string is passed, it will be passed as an 
		argument to another function and from there interactivity will be executed
		
	*/
	
	$data="";
	if(isset($dataSet["lines"])){
		foreach($dataSet["lines"] as $v){
			$data[]=[
						"data"=>$v,
						"lines"=> [ "show"=> true, ],
						"color"=>["#EEEEEE", "#00BA8B"],
						"curvedLines"=>["apply"=>true],
						"hoverable"=>false
						
					];
			$data[]=[
						"data"=>$v,
						"lines"=> [ "show"=> false, ],
						"points"=> [ "show"=> true ],
						"color"=>["#EEEEEE", "#00BA8B"],
					];
		}
	}
	if(isset($dataSet["bars"])){
		foreach($dataSet["bars"] as $v){
			$data[]=[
						"data"=>$v,
						"bars"=> [ "show"=> true ]
					];
		}
	}
	
	$data=json_encode($data);
	$html=<<<EOT

			
			<script>
			$(document).ready(function(){
				var placeholder = $("#{$id}");
				options_{$id}={
								grid: {
											hoverable: true,
											clickable: true,
											borderColor: "#EEEEEE",
											borderWidth: 1,
											label:"hello"
										},
								series:{
											curvedLines: { active: true	}
									},
								tooltip: true,
								tooltipOpts: {
									content: "X:%x | Y:%y",
									shifts: {
										x: -60,
										y: 25
									}
								}
								
						};
				if('{$timeLine}' && '{$timeLine}'==1){
					var options_{$id} = {
								xaxis: {
										mode: "time",
										tickLength: 5
									},
								grid: {
										hoverable: true,
										clickable: true,
										borderColor: "#EEEEEE",
										borderWidth: 0,
									},
								series:{
											curvedLines: { active: true	}
									},
								tooltip: true,
								tooltipOpts: {
									content: "X:%x | Y:%y",
									shifts: {
										x: -60,
										y: 25
									}
								}
							};
				}
				//console.log(data);
				$.plot("#{$id}", {$data}, options_{$id});
				$("#{$id}").bind("plotclick", function(event, pos, obj) {

					if('{$onClickArgument}'!="")	interactiveLineCallBack('{$onClickArgument}',event,pos,obj);
				});
			});
			function interactiveLineCallBack(arg,event,pos,obj){
			}
		</script>
		<div id="{$id}" style="height:{$height}; padding-left:10px; width:{$width}; font-family:arial"></div>

	
EOT;
	return $html;
}
	
	function getBarGraph($data,$id,$width="1100",$height="250"){
	
		$data=json_encode($data);
		$html=<<<EOT
			<script>
				$(document).ready(function(){
					var barChartData = {
						datasets: [
							{
								fillColor: "rgba(220,220,220,0.5)",
								strokeColor: "rgba(220,220,220,1)",
								data: jQuery.parseJSON('{$data}')
							},
						]
					}

					var myLine = new Chart(document.getElementById("{$id}").getContext("2d")).Bar(barChartData);
				});
			</script>
			<canvas id="$id" class="chart-holder" width="{$width}" height="{$height}"></canvas>
EOT;
		return $html;
	}
	
	function getDoughnutGraph($x,$max,$id,$width="50",$height="50"){
	
		if($max==0) return;
		//$y = $max-$x;
        $colorCodes=[
                        '#00BA8B',
                        '#F7464A',
                        '#CD2727',
                        '#CD27B9',
                        '#9B27CD',
                        '#4A3553',
                        '#510C1C',
                        '#612DAA',
                        '#2D61AA',
                        '#2DAA59',
                        '#1CE715',
                        '#D4F23E',
                ];
        $data=NULL;
        $xTotal=0;
        $k=NULL;
        foreach($x as $k=>$v){
            $xTotal=$xTotal+$v;
            $data[$k]=["value"=>$v,"color"=>$colorCodes[$k]];
        }
        $k=$k+1;
        if($xTotal<$max){
            $v=$max-$xTotal;
            $data[$k]=["value"=>$v,"color"=>'#E2EAE9'];
        }
		$data=json_encode($data);
		$html=<<<EOT
			<script>
				$(document).ready(function(){
                    var data=jQuery.parseJSON('{$data}');
					new Chart(document.getElementById("{$id}").getContext("2d")).Doughnut(data); // ,options
				});
			</script>
			<canvas id="$id" class="chart-holder" width="{$width}" height="{$height}"></canvas>
EOT;
		return $html;
	}
	function getLineGraph_jq_to_flot_conv($ds1,$ds2,$id="dummy",$width="1100",$height="250",$sort=TRUE,$onClickArgument="",$timeLine=false){
		$data=NULL;
		$width="{$width}px";
		$height="{$height}px";
		foreach($ds1 as $k=>$v){
			$data[$k]=[$ds1[$k],$ds2[$k]];
		}
		$dataSet=[
				"lines"=>[$data]
				];
		return getLineGraph_flot($dataSet, $id, $onClickArgument, $height, $width);
		
	
	}
?>